package com.clinic.action.home;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.action.common.AuthenticationAction;
import com.clinic.entity.gui.GuiAppointment;
import com.clinic.entity.gui.GuiDrugStock;
import com.clinic.entity.gui.GuiPatient;
import com.clinic.entity.param.Index;
import com.clinic.entity.param.SessionUser;
import com.clinic.entity.type.UserType;
import com.clinic.logic.appointment.AppointmentLogic;
import com.clinic.logic.drug.DrugStockLogic;
import com.clinic.logic.user.AssistantLogic;
import com.clinic.logic.user.DoctorLogic;
import com.clinic.logic.user.PatientLogic;
import com.clinic.util.CommonUtil;

public class HomeAction extends AuthenticationAction {

	private static final long serialVersionUID = -4796862475958572512L;
	
	private String urlSuffix;
	private GuiPatient guiPatient;
	private GuiAppointment guiAppointment;
	private List<GuiAppointment> guiAppointmentList;
	private List<GuiDrugStock> guiDrugStockList;
	
	@Autowired
	private AppointmentLogic appointmentLogic;
	@Autowired
	private PatientLogic patientLogic;
	@Autowired
	private DoctorLogic doctorLogic;
	@Autowired
	private AssistantLogic assistantLogic;
	@Autowired
	private DrugStockLogic drugStockLogic;
	
	@Override
	 public void prepare() {
	  super.prepare();
	 }
	
	public String execute() {
		setUserUrlSuffix();
		return ActionResult.SUCCESS;
	}
	
	public String general() {
		return ActionResult.SUCCESS;
	}
	
	public String doctor() {
		if(isDoctorLogin()) {
			Date date = CommonUtil.getTodayDate();
			guiAppointmentList = appointmentLogic.getDayAppointmentListByDate(date);
			guiDrugStockList = drugStockLogic.getOutDatedDrugStocksByDate(date, new Index(100));
			return ActionResult.SUCCESS;
		} else {
			addActionError("Authentication failed");
			return ActionResult.AUTH_FAILED;
		}		
	}
	
	public String assistant() {
		if(isAssistantLogin()) {
			Date date = CommonUtil.getTodayDate();
			guiAppointmentList = appointmentLogic.getDayAppointmentListByDate(date);
			guiDrugStockList = drugStockLogic.getOutDatedDrugStocksByDate(date, new Index(100));
			return ActionResult.SUCCESS;
		} else {
			addActionError("Authentication failed");
			return ActionResult.AUTH_FAILED;
		}	
	}
	
	public String patient() {
		if(isPatientLogin()) {
			SessionUser sessionUser =  getLoginUser();
			guiPatient = patientLogic.getPatientDetailsById(sessionUser.getUserId());
			guiAppointment = appointmentLogic.getUpcomingPatientAppointment(guiPatient.getUserId(), CommonUtil.getTodayDate());
			return ActionResult.SUCCESS;
		} else {
			addActionError("Authentication failed");
			return ActionResult.AUTH_FAILED;
		}	
	}
	
	private void setUserUrlSuffix() {
		if (getLoginUser().isAvailable()) {
			UserType type = getLoginUser().getType();
			switch (type) {
			case DOCTOR:
				urlSuffix = "doctor";
				break;
			case ASSISTANT:
				urlSuffix = "assistant";
				break;
			case PATIENT:
				urlSuffix = "patient";
				break;
			default:
				urlSuffix = "general";
			}
		} else {
			urlSuffix = "general";
		}
	}

	public String getUrlSuffix() {
		return urlSuffix;
	}

	public GuiPatient getGuiPatient() {
		return guiPatient;
	}

	public void setGuiPatient(GuiPatient guiPatient) {
		this.guiPatient = guiPatient;
	}
	
	public boolean isExistGuiAppointment() {
		return guiAppointment != null && guiAppointment.isAvailable();
	}
	
	public GuiAppointment getGuiAppointment() {
		return guiAppointment;
	}

	public void setGuiAppointment(GuiAppointment guiAppointment) {
		this.guiAppointment = guiAppointment;
	}

	public boolean isExistGuiAppointmentList() {
		return (guiAppointmentList != null && guiAppointmentList.size() > 0);
	}

	public List<GuiAppointment> getGuiAppointmentList() {
		return guiAppointmentList;
	}

	public void setGuiAppointmentList(List<GuiAppointment> guiAppointmentList) {
		this.guiAppointmentList = guiAppointmentList;
	}

	
	public boolean isExistGuiDrugStockList() {
		return (guiDrugStockList != null && guiDrugStockList.size() > 0);
	}

	
	public List<GuiDrugStock> getGuiDrugStockList() {
		return guiDrugStockList;
	}

	public void setGuiDrugStockList(List<GuiDrugStock> guiDrugStockList) {
		this.guiDrugStockList = guiDrugStockList;
	}

}
